﻿using System;
using System.Globalization;
using System.Linq;
using System.Windows.Data;
using Dispatcher.Helpers;
using Dispatcher.Views;
using Repository;

namespace Dispatcher.Converters
{
    /// <summary>
    /// Lazy Load Converter.
    /// </summary>
    public class EventTooltipConverter : IValueConverter
    {
        /// <summary>
        /// Converts a value.
        /// </summary>
        /// <param name="value">The value produced by the binding source.</param>
        /// <param name="targetType">The type of the binding target property.</param>
        /// <param name="parameter">The converter parameter to use.</param>
        /// <param name="culture">The culture to use in the converter.</param>
        /// <returns>
        /// A converted value. If the method returns null, the valid null value is used.
        /// </returns>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var ev = value as Event;
            if (ev != null)
            {
                if (ev.Id == 0)
                {
                    return string.Format("Свободно с {0} до {1}.",
                                         ev.StartTime.ToShortTimeString(),
                                         ev.EndTime.ToShortTimeString());
                }

                if (ev.OrganizePersonGroup != null)
                {
                    var person = ev.OrganizePersonGroup.Person.FirstOrDefault();
                    var personName = string.Empty;
                    if (person != null)
                    {
                        personName = ConverterContainer.PersonFullNameConverter.Convert(person, targetType, parameter, culture) as string;
                    }                    

                    var group = ev.EventGroup.FirstOrDefault(g => g.EventGroupType.Name == "Class");
                    var groupName = string.Empty;
                    if (@group != null)
                    {
                        groupName = @group.Name;
                    }

                    var description = "нет описания" + Environment.NewLine;
                    if (string.IsNullOrEmpty(ev.Name))
                    {
                        description = ev.Name;
                    }

                    var result = string.Format(
                        "Дата: {4}" + Environment.NewLine +
                        "Место: {6}" + Environment.NewLine + 
                        "Время: {0}-{1}" + Environment.NewLine +
                        "Организатор: {2}" + Environment.NewLine +
                        "Тип: {3}" + Environment.NewLine +
                        "Описание: {5}",
                        ev.StartTime.ToShortTimeString(),
                        ev.EndTime.ToShortTimeString(),
                        personName, groupName,
                        ev.StartTime.Date.ToShortDateString(),
                        description, ev.Place.Name + " " + ev.Place.Location.Name);

                    var weeklyEventGroups =
                        ev.EventGroup.Where(g => g.EventGroupType.Name == MainWindow.EventGroupTypeSet.Periodic.Name).
                            ToList();
                    if (weeklyEventGroups.Count>0)
                    {
                        result += "Переодичность: " + Environment.NewLine;
                        result = weeklyEventGroups.Aggregate(result, (current, evenntGroup) => current + ("\t" + evenntGroup.Name + Environment.NewLine));
                    }

                    return result;
                }
            }

            return null;
        }

        /// <summary>
        /// Converts a value.
        /// </summary>
        /// <param name="value">The value that is produced by the binding target.</param>
        /// <param name="targetType">The type to convert to.</param>
        /// <param name="parameter">The converter parameter to use.</param>
        /// <param name="culture">The culture to use in the converter.</param>
        /// <returns>
        /// A converted value. If the method returns null, the valid null value is used.
        /// </returns>
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
